﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SmartRS.com.vn.smartrs.domain;
using SmartRS.com.vn.smartrs.service;
using SmartRS.com.vn.smartrs.util;
using SmartRS.com.vn.smartrs.dao;

namespace SmartRS.com.vn.smartrs.form.menu
{
    public partial class frmTransferDesk : Form
    {
        List<Hall> listHall = new List<Hall>();
        List<Desk> listDesk = new List<Desk>();
        long srcDeskId = 0L;
        public long destDeskId {get; private set;}

        public frmTransferDesk()
        {
            InitializeComponent();
        }

        public frmTransferDesk(long deskId, IList<Hall> lstHall)
        {
            InitializeComponent();
            this.listHall = lstHall.ToList();
            this.srcDeskId = deskId;
        }

        private void frmTransferDesk_Load(object sender, EventArgs e)
        {
            Desk desk = DeskService.getInstance().getById(srcDeskId);
            if (desk == null) { return;}
            Hall hall = HallService.getInstance().getById(desk.hall.hallId);
            if (hall == null) { return; }
            String srcName = "Bàn " + desk.deskName.ToString() + " - Sảnh " + hall.hallName.ToString();
            lblSrcDesk.Text = srcName;

            cboHall.DataSource = listHall;
            cboHall.DisplayMember = HallDao.HALL_NAME;
            cboHall.ValueMember = HallDao.HALL_ID;

            if (cboHall!= null && cboHall.Items.Count > 0)
            {
                cboHall.SelectedIndex = 0;
                getDataCboDesk(((Hall)cboHall.Items[0]).hallId);
            }
        }

        private void cboHall_SelectedIndexChanged(object sender, EventArgs e)
        {
            long hallId;
            if (cboHall.SelectedValue == null 
                || long.TryParse(cboHall.SelectedValue.ToString(),out hallId) == false) { return; }
            getDataCboDesk(hallId);
        }

        private void getDataCboDesk(long hallId)
        {
            try
            {
                listDesk.Clear();
                //hallId = long.Parse(cboHall.SelectedValue.ToString());
                IList<Desk> rs = DeskService.getInstance().getListDeskByHallId(hallId);
                if (rs == null || rs.Count <= 0) { return; }
                cboDesk.Items.Clear();
                foreach (Desk d in rs)
                {
                    if (d.status == tableState.free)
                    {
                        cboDesk.Items.Add(d);
                        //                       listDesk.Add(d);
                    }

                }
                //cboDesk.DataSource = listDesk;
                cboDesk.DisplayMember = DeskDao.DESK_NAME;
                cboDesk.ValueMember = DeskDao.DESK_ID;
                if (cboDesk.Items.Count > 0)
                {
                    cboDesk.SelectedItem = cboDesk.Items[0];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private void btnTransfer_Click(object sender, EventArgs e)
        {
            if (cboDesk.SelectedItem != null)
            {
                destDeskId = int.Parse(((Desk)cboDesk.SelectedItem).deskId.ToString());
            }
        }

    }
}
